package oa.hleast.xswl.controller.admin.article;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.xswl.pojo.domain.article.CmsArticle;
import oa.hleast.xswl.pojo.form.article.CmsArticleForm;
import oa.hleast.xswl.pojo.form.article.CmsArticleReviewFrom;
import oa.hleast.xswl.pojo.vo.article.CmsArticleVo;
import oa.hleast.xswl.service.article.IInformationCentreService;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author 302
 * @description
 * @date 2021/3/10
 */
@Api(tags = "【系统管理】资讯中心")
@RestController("InformationCentreController")
@RequestMapping("/api.admin/v1/information")
@AllArgsConstructor
@Slf4j
public class InformationCentreController {

    private final IInformationCentreService informationCentreService;


    @ApiOperation(value = "新增资讯", httpMethod = "POST")
    @ApiImplicitParam(name = "articleForm", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsArticleForm",dataTypeClass = CmsArticleForm.class)
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody @NotNull CmsArticleForm articleForm) {
        boolean status = informationCentreService.add(articleForm);
        return Result.judge(status);
    }

    @ApiOperation(value = "修改资讯", httpMethod = "PUT")   //TODO 需要修改这个方法
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "article", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsArticle",dataTypeClass = CmsArticle.class)
    })
    @PutMapping(value = "update/{id}")
    public Result<Boolean> update(
            @PathVariable @NonNull Integer id,
            @RequestBody @NonNull CmsArticleForm articleFrom) {
        articleFrom.setGmtModified(new Date());
        boolean status = informationCentreService.update(articleFrom);
        return Result.judge(status);
    }

    @ApiOperation(value = "列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式 page分页查询", paramType = "query", dataType = "QueryModeEnum",dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "reviewStatus", value = "审核状态(1未审核，10审核通过，11审核驳回)", paramType = "query", dataType = "String",dataTypeClass = String.class),
            @ApiImplicitParam(name = "dictItemInformationId", value = "资讯类型id(新闻资讯)", paramType = "query", dataType = "String",dataTypeClass = String.class),
            @ApiImplicitParam(name = "title", value = "资讯名称", paramType = "query", dataType = "String",dataTypeClass = String.class)
    })
    @GetMapping("query-list")
    public Result<List<CmsArticleVo>> list(
            @RequestParam @NotBlank String queryMode,
            @RequestParam @NonNull Integer page,
            @RequestParam @NonNull Integer limit,
            @RequestParam String title,
            @RequestParam Integer reviewStatus,
            @RequestParam String dictItemInformationId) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        Page<CmsArticleVo> result = null;
        switch (queryModeEnum) {
            case PAGE:
                result = informationCentreService.pageList(page, limit, title, reviewStatus, dictItemInformationId);
                return Result.success(result.getRecords(), result.getTotal());
            default:
                throw new BizException("资讯类型错误，请联系管理员");
        }
    }

    @ApiOperation(value = "资讯详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "资讯id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("get-details/{id}")
    public Result<CmsArticleVo> detail(@PathVariable Long id) {
        CmsArticleVo advert = informationCentreService.detail(id);
        return Result.success(advert);
    }

    @ApiOperation(value = "删除资讯", httpMethod = "DELETE")            //TODO 删除功能，测试无法实现
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    @DeleteMapping("delete/{ids}")
    public Result<Boolean> delete(@PathVariable("ids") String ids) {
        boolean status = informationCentreService.deleteByIds(Arrays.asList(ids.split(",")));
        return Result.judge(status);
    }

    @ApiOperation(value = "资讯审核", httpMethod = "PUT")       //TODO 参数不一致，需要优化修改
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "reviewStatus", value = "审核状态", required = true, paramType = "query", dataType = "Integer",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "reason", value = "驳回原因", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)

    })
    @PutMapping("review/{id}")
    public Result<Boolean> review(@PathVariable("id") Long id, @RequestBody @NonNull CmsArticleReviewFrom cmsArticleReviewFrom) {
        boolean status = informationCentreService.review(id, cmsArticleReviewFrom.getReviewStatus(), cmsArticleReviewFrom.getReason());
        return Result.judge(status);
    }

    @ApiOperation(value = "所有数据 -供其他服务调用", httpMethod = "GET")
    @GetMapping("findAll")
    public Result<List<CmsArticle>> findAll() {
        List<CmsArticle> result = informationCentreService.list(new LambdaQueryWrapper<CmsArticle>().orderByDesc(CmsArticle::getGmtCreate));
        return Result.success(result);
    }

    @ApiOperation(value = "指定时间区间内的所有数据 -供其他服务调用", httpMethod = "GET")
    @GetMapping("findAllBetweenDate")
    public Result<List<CmsArticle>> findAllBetweenDate(@RequestParam @NonNull Long fromDate, @RequestParam @NotNull Long endDate) {
        List<CmsArticle> result = informationCentreService.list(new LambdaQueryWrapper<CmsArticle>()
                .between(CmsArticle::getGmtCreate, fromDate, endDate)
                .orderByDesc(CmsArticle::getId));
        return Result.success(result);
    }
}
